from typing import Any, List

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi_pagination import Page
from pydantic.types import UUID4
from sqlalchemy.orm import Session

from app import crud, models, schemas
from app.api import deps

router = APIRouter()


@router.get("/", response_model=Page[schemas.Group], summary="检索Groups")
def read_groups(
        db: Session = Depends(deps.get_db),
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    检索当前用户的 Groups。管理员用户检索所有 Groups。
    """
    if current_user.is_admin:
        groups = crud.group.get_paginate(db=db)
    else:
        groups = crud.group.get_paginate_link_user_group(db=db, user=current_user)

    return groups


@router.post("/", response_model=schemas.Group, summary="创建新Group")
def create_group(
        *,
        db: Session = Depends(deps.get_db),
        group_in: schemas.GroupCreate,
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    创建新 Group
    """
    group = crud.group.create_link_user_group(db=db, obj_in=group_in, user=current_user)
    return group


# @router.post("/admin", response_model=schemas.Group, summary="创建新Group (管理员)")
# def create_Group_admin(
#         *,
#         db: Session = Depends(deps.get_db),
#         group_in: schemas.GroupCreate,
#         _: models.User = Depends(deps.get_current_admin_user),
#         user_id: UUID4,
# ) -> Any:
#     """
#     ADMIN: 为其他用户创建 Group
#     """
#     user = crud.user.get(db=db, id=user_id)
#     if not user:
#         raise HTTPException(
#             status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
#         )
#     group = crud.group.create_with_user(db=db, obj_in=group_in, user=user)
#     return group


@router.put("/{id}", response_model=schemas.Group, summary="更新Group")
def update_group(
        *,
        db: Session = Depends(deps.get_db),
        id: UUID4,
        group_in: schemas.GroupUpdate,
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    更新 Group.
    """
    group = crud.group.get(db=db, id=id)
    if not group:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
        )
    if not current_user.is_admin and (group.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    group = crud.group.update_link_user_group(db=db, db_obj=group, obj_in=group_in)
    return group


@router.get("/{id}", response_model=schemas.Group, summary="获取Group")
def read_group(
        *,
        db: Session = Depends(deps.get_db),
        id: UUID4,
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    按 ID 获取 Group.
    """
    group = crud.group.get(db=db, id=id)
    if not group:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
        )
    if not current_user.is_admin and (group.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    return group


@router.delete("/{id}", response_model=schemas.Group, summary="删除Group")
def delete_group(
        *,
        db: Session = Depends(deps.get_db),
        id: UUID4,
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    按 ID 删除 Group.
    """
    group = crud.group.get(db=db, id=id)
    if not group:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
        )
    if not current_user.is_admin and (group.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    group = crud.group.remove(db, group)
    return group
